﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using AAA.Forms.Components.MetaData;
using AAA.DataLoader;
using AAA.Database;
using AAA.Base.Util.Hook;
using AAA.Base.Util;
using AAA.Quote;
using AAA.Meta.Quote.Data;
using AAA.Meta.Quote;

namespace AAA.FormTest
{
    public partial class Form1 : Form
    {
        private Color[] _cColors = {Color.Red, Color.White, Color.Yellow, Color.Green, Color.Gray };
        private GlobalHook _globalHook;

        public Form1()
        {
            InitializeComponent();
            _globalHook = new GlobalHook();
            _globalHook.KBHookInvoked += new GlobalHook.KBHookEventHandler(_globalHook_KBHookInvoked);
            _globalHook.Install();

            List<DateTime> lstBarTime = KBarHelper.GenerateBarTime("08:45:00", "13:45:00", DateTime.Now, new BarCompression(BarCompressionEnum.Min, 4));
        }

        void _globalHook_KBHookInvoked(object sender, KBHookEventArgs e)
        {
            lblPress.Text = e.lParam + "," + e.wParam + "," + e.HookCode;
        }

        private void tStart_Tick(object sender, EventArgs e)
        {
            string[] strValues;
            LabelMeta lblMeta;
            try
            {
                strValues = txtValue.Text.Split(' ');
                lblLabel.IsUpdateText = false;
                lblLabel.ClearText();
                for (int i = 0; i < strValues.Length; i++)
                {
                    lblMeta = new LabelMeta();
                    lblMeta.Text = strValues[i];
                    lblMeta.ForeColor = _cColors[i % _cColors.Length];
                    lblLabel.AddText(lblMeta);
                }
                lblLabel.IsUpdateText = true;
            }
            catch (Exception ex)
            {
                
            }
        }

        private void btnStart_Click(object sender, EventArgs e)
        {
            tStart.Enabled = !tStart.Enabled;
        }

        private void btnLoad_Click(object sender, EventArgs e)
        {
            CSVLoader csvLoader = new CSVLoader();
            try
            {
                csvLoader.Database = new MSSqlDatabase();
                csvLoader.Database.Open(txtHost.Text, txtDatabase.Text, txtUsername.Text, txtPassword.Text);
                csvLoader.SQLStatement = txtSQL.Text;
                csvLoader.SplitChar = '\t';
                csvLoader.HasHeader = true;
                csvLoader.Load(txtFilename.Text);
                if(csvLoader.ErrorMessage != null)
                    MessageBox.Show(csvLoader.ErrorMessage);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message + "," + ex.StackTrace);                    
            }
        }

        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            _globalHook.Uninstall();
        }

        private void btnSplit_Click(object sender, EventArgs e)
        {
            try
            {
                IOHelper.SplitFile(txtSourceFile.Text, txtTargetFile.Text, 100000);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message + "\n" + ex.StackTrace);
            }
        }
    }
}
